package cn.itcast.user.web;

import cn.itcast.user.pojo.User;
import cn.itcast.user.properties.UserServiceProperties;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope //支持nacos配置热更新(动态获取配置无需重启服务) 方式一
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${pattern.dateformat}")// 从nacos配置中心获取配置
    private String dateformat;
    @Autowired
    private UserServiceProperties userServiceProperties;

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {
        return userService.queryById(id);
    }
    @GetMapping("/now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat, Locale.CHINA));
    }
    @GetMapping("/prop")
    public UserServiceProperties nowTow(){
        return userServiceProperties;
    }
    @GetMapping("/ant")
    public String ant(@RequestHeader String ant){
        return ant;
    }
}
